package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserDynamicMaintenanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author: xy
 * @Date: create in 2020/3/8
 * @description:
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/UserDynamicMaintenance")
public class UserDynamicMaintenanceController {

    @Reference
    UserDynamicMaintenanceService userDynamicMaintenanceService;

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("controller{"+queryPageBean+"}");
        PageResult pageResult = userDynamicMaintenanceService.findByPage(queryPageBean);
        log.debug("用户权限动态查询!!");
//        return null;
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody SysUser sysUser, Integer[] roleIds){
        log.debug("测试打通新增前端页面===========sysUser"+sysUser.toString());
//            sysUser.setPassword(MD5Utils.getPwd(sysUser.getPassword()));
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);
        userDynamicMaintenanceService.add(sysUser,roleIds);
        return new Result(true,"新增权限成功");
    }
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("测试打通删除,获取id============"+id);
        userDynamicMaintenanceService.delById(id);

        return new Result(true,"删除成功");
    }
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("打通前端页面获取id========"+id);
       SysUser sysUser = userDynamicMaintenanceService.findById(id);
       log.debug("是否得到数据=======sysUser:"+sysUser);
       return new Result(true,"根据id查询成功",sysUser);
    }

    @RequestMapping("/findRoleIdsById")
    public Result findRoleIdsById(Integer id){
        List<Integer> roleIds = userDynamicMaintenanceService.findRoleIdsById(id);
        return new Result(true,"根据用户Id查角色id成功!",roleIds);
    }
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody SysUser sysUser,Integer[] roleIds ){
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);
        log.debug("打通前端页面============sysUser:"+sysUser);
        userDynamicMaintenanceService.edit(sysUser,roleIds);
        return new Result(true,"更新成功!");
    }
}
